package com.gallsoft.games.bennyadv;

import com.gallsoft.games.framework.gamedev2d.DynamicGameObject;
import com.gallsoft.games.framework.math.Rectangle;

public class Bob extends DynamicGameObject {

    public static final int BOB_STATE_JUMP = 0;
    public static final int BOB_STATE_FALL = 1;
    public static final int BOB_STATE_HIT = 2;
    public static final int BOB_STATE_WALK = 3;
    public static final int BOB_STATE_STOP = 4;
    public static final int BOB_STATE_CLIMB = 5;
    public static final int BOB_STATE_MP = 6;
    //public static final float BOB_JUMP_VELOCITY = 11;    
    //public static final float BOB_MOVE_VELOCITY = 20;
    public static final float BOB_JUMP_VELOCITY = 8.5f;
    public static final float BOB_MOVE_VELOCITY = 2.5f;
    //public static final float BOB_WIDTH = 0.8f;
    //public static final float BOB_HEIGHT = 0.8f;
    public static final float BOB_WIDTH = 1.25f;
    public static final float BOB_HEIGHT = 1.25f;

    int state, facing;
    float stateTime;
    float lastYPosition;

    public Bob(float x, float y) {
        super(x, y, BOB_WIDTH, BOB_HEIGHT);
        
        state = BOB_STATE_STOP;
        stateTime = 0;
        bounds = new Rectangle((x + 0.25f - BOB_WIDTH / 2), y - BOB_HEIGHT / 2, BOB_WIDTH - 0.5f, BOB_HEIGHT);
    }

    public void setStartPosition(float x, float y, int facing) {
        this.position.x = x;
        this.position.y = y;
        this.facing = facing;
        
        bounds = new Rectangle((x + 0.25f - BOB_WIDTH / 2), y - BOB_HEIGHT / 2, BOB_WIDTH - 0.5f, BOB_HEIGHT);        
    }
    
    public void update(float deltaTime, World.BobNextState newState) {
        if (state != BOB_STATE_CLIMB)
            velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
        position.add(velocity.x * deltaTime, velocity.y * deltaTime);
 
        //if (velocity.x != 0 && state != BOB_STATE_JUMP) {
            if (newState == World.BobNextState.WALK  && state != BOB_STATE_WALK) {
                state = BOB_STATE_WALK;
                stateTime = 0;
            }                
        //}
            //else if (velocity.x == 0 && state != BOB_STATE_JUMP && state != BOB_STATE_CLIMB && state != BOB_STATE_HIT) {
            else if (newState == World.BobNextState.STOP && state != BOB_STATE_HIT) {
            if (state != BOB_STATE_STOP) {
                state = BOB_STATE_STOP;
                stateTime = 0;
            }                
        }

        if (newState == World.BobNextState.CLIMB && state != BOB_STATE_HIT) {
            if (state != BOB_STATE_CLIMB) {
                state = BOB_STATE_CLIMB;
                stateTime = 0;
            }
        }
        
        //if (velocity.y > 0 && state != BOB_STATE_HIT) {
        if (newState == World.BobNextState.JUMP && state != BOB_STATE_HIT) {
            if (state != BOB_STATE_JUMP) {
                state = BOB_STATE_JUMP;
                stateTime = 0;
            }                
        }
        
        
        bounds.lowerLeft.set(position).sub(bounds.width / 2, bounds.height / 2);

        // Check for edge of the world
        if (position.x <= 0.5f)
            position.x = 0.5f;
        if (position.x > World.WORLD_WIDTH - BOB_WIDTH)
            position.x = World.WORLD_WIDTH - BOB_WIDTH;

        if (velocity.x < 0)
            facing = -1;
        else if (velocity.x > 0)
            facing = 1;
        
        stateTime += deltaTime;
    }

    /*
     * public void hitSquirrel() { 
     *      velocity.set(0,0); 
     *      state = BOB_STATE_HIT;
     *      stateTime = 0; 
     * }
     */

    public void hitEnemy() { 
        velocity.set(0, 5.0f); 
        state = BOB_STATE_HIT;
        stateTime = 0; 
    }

    public boolean hitPlatform(Platform platform) {
        velocity.y = 0;
        //velocity.x += platform.velocity.x;
        
        if (state == BOB_STATE_JUMP || state == BOB_STATE_FALL) {
            state = BOB_STATE_STOP;
            stateTime = 0;
            velocity.x = 0;
        }        
        
        position.y = platform.position.y + (Platform.PLATFORM_HEIGHT / 2) + (BOB_HEIGHT / 2);

        if (platform.velocity.x != 0) {
            //state = BOB_STATE_MP;
            //velocity.x = platform.velocity.x;
            return true;
        }
        else
            return false;
    }

    public void hitSolidPlatform(Platform platform) {
            position.y = platform.position.y - (Platform.PLATFORM_HEIGHT / 2) - (BOB_HEIGHT / 2);
            state = Bob.BOB_STATE_FALL;
            velocity.y = 0;       
    }
    
    public void hitVerticalSolidBlock(SolidBlock solidBlock) {
        if (velocity.x > 0) {
            position.x = solidBlock.position.x - (SolidBlock.BLOCK_WIDTH / 2) - (BOB_WIDTH / 2);
            velocity.x = 0;
        }
        else if (velocity.x < 0) {
            position.x = solidBlock.position.x + (SolidBlock.BLOCK_WIDTH / 2) + (BOB_WIDTH / 2);
            velocity.x = 0;
        }

    }
    
    public void hitDoor(Scenery door) {
        if (velocity.x > 0) {
            position.x = door.position.x - (SolidBlock.BLOCK_WIDTH / 2) - (BOB_WIDTH / 2);
            velocity.x = 0;
        }
        else if (velocity.x < 0) {
            position.x = door.position.x + (SolidBlock.BLOCK_WIDTH / 2) + (BOB_WIDTH / 2);
            velocity.x = 0;
        }

    }
    
    public void hitLadderBlock(LadderBlock ladderBlock) {
        position.x = ladderBlock.position.x;
        state = Bob.BOB_STATE_CLIMB;
        velocity.x = 0;
    }

    public void offLadderBlock() {
        //position.y = platform.position.y - (Platform.PLATFORM_HEIGHT / 2) - (BOB_HEIGHT / 2);
        state = Bob.BOB_STATE_STOP;
        velocity.y = 0;
    }
    
    public void setFalling() {
        if (state != BOB_STATE_FALL) {
            state = BOB_STATE_FALL;
            stateTime = 0;
        }
    }
    
}
